<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/18
 * Time: 13:59
 */

class imeictl extends MY_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('imei_model');
    }
    public function index(){
        $this->imeilist();
    }

    /**
     * 查询
     */
    public function imeilist($page='',$type=''){
        $gp=gp();
        if($page==''){
            $page=1;
        }
        $this->load->library('pagination');
        $config['base_url'] = blang_url('imei/imeictl/imeilist/');
        $config['total_rows'] = $this->imei_model->countRows('imei_warranty_date');
        $config['uri_segment'] = config_item('enable_lang')?5:4;
        $config['per_page'] = 20;

        $this->pagination->initialize($config);

        $data['pageshow']= $this->pagination->create_links();
        $data['page']=$page;
        $data['imeilist']=$this->imei_model->imeilist($gp['keyword'],$gp['startTime'],$gp['endTime'],$config['per_page'],($page-1)*$config['per_page']);
        if($type=='dachuyes') {
            $this->pexcel($this->imei_model->imeilist());
        }
        $this->load->view('defaults/imei_list',$data);
    }

    /**
     * 导出excel
     */
    public function pexcel($newlist){
        $fieldData = array(
            'id'=>'id',
            'sku'=>'sku',
            'imei_1'=>'imei_1',
            'imei_2'=>'imei_2',
            'warranty_start_date'=>'warranty_start_date',
            'warranty_end_date'=>'warranty_end_date',
        );
        excel_output('imiedataexcel',$fieldData,$newlist);
    }

    /**
     * 导入excel
     */
    public function uploadimei(){
        $uploaddir="./resource/uploadfiles/file/excels/imei/";
        $type=array('xlsx','xls');
        $up=uploadfile($_FILES['fileField'],$uploaddir,$type);
        if($up){
            $data=ExcelToArray($up);
            $num=count($data[0]);
            if(!is_numeric($data[1][2]) || !is_numeric($data[1][3]) || $num != 6){
                js_goto('','请上传合格的EXCEL文件！！','1');
            }else {
                for ($i = 1; $i < count($data); $i++) {
                    $arr = array(
                        'sku' => $data[$i][1],
                        'imei_1' => $data[$i][2],
                        'imei_2' => $data[$i][3],
                        'warranty_start_date' => $data[$i][4],
                        'warranty_end_date' => $data[$i][5],
                    );
                    $this->imei_model->addimei($arr);
                }
                $this->imeilist();
            }
        }
    }

    /**
     * 获取IMEI
     */
    public function GetImei(){
        $gp=gp();
        if($gp) {
            $url = 'http://stock.dhhtgroup.com/webService/GetImeiHistory.php';
            $data = array('account' => 'pandWill', 'token' => 'pandWillImei', 'imei' => $gp['imei']);
            $result = CurlPost($url, $data);
            $data['ImeiInfo'] = $result;
        }else{
            $data['ImeiInfo']=array();
        }
        $this->load->view('defaults/imei/ImeiApi',$data);
    }
}